﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSBusinessEntities;
using System.Transactions;
using MBSBLC;
using System.Web;
using MBSProcessComponent;

namespace MBSBLC
{

    public partial class BLC

    {

        public static string ErrorMessage;

        public Dictionary<int, string> GetShowTimeByMoviesAndCinemaAndTheatreNo(int MovieID, int CinemaID, int ScreenNo)
        {

            ShowTimeBE showTimeBE = new ShowTimeBE();

            List<ShowTimeBE> showTimeBEList = showTimeBE.GetShowTimeByMovieAndCinemaAndTheatreNo(MovieID, CinemaID, ScreenNo);

            Dictionary<int, string> showTimeDictionary = new Dictionary<int, string>();

            foreach (ShowTimeBE showTimeBETemp in showTimeBEList)
            {

                string showTimeDisplay = showTimeBE.ShowDateTime.ToString("HH:MMtt");
                showTimeDictionary.Add(showTimeBETemp.ShowID, showTimeDisplay);
            }

            return showTimeDictionary;

        }

        /*
        public Dictionary<string, Dictionary<int, string>> GetShowTimeByMoviesAndCinema(int MovieID, int CinemaID)
        {

            ShowTimeBE showTimeBE = new ShowTimeBE();

            List<ShowTimeBE> showTimeBEList = showTimeBE.GetShowTimeByMovieAndCinema(MovieID, CinemaID);
            Dictionary<string, Dictionary<int, string>> showTimeGroupDictionary = new Dictionary<string, Dictionary<int, string>>();
            foreach (ShowTimeBE ShowTimeBE in showTimeBEList){
                string dateKey = ShowTimeBE.ShowDateTime.ToString("D");
                string showTimeDisplay = ShowTimeBE.ShowDateTime.ToString("HH:MMtt");
                Dictionary<int, string> showTimeDictionary;
                if (showTimeGroupDictionary.ContainsKey(dateKey))
                {
                    showTimeDictionary = showTimeGroupDictionary[dateKey];
                    showTimeGroupDictionary.Remove(dateKey);
                }
                else
                {
                   showTimeDictionary = new Dictionary<int,string>();      
                }
                showTimeDictionary.Add(ShowTimeBE.ShowID, showTimeDisplay);
                showTimeGroupDictionary.Add(dateKey, showTimeDictionary);
            }

            return showTimeGroupDictionary;
          
        }
        */

        
        public bool blockAvailableSeatsInCache(int showTimeId,List<String> seatList)
        {
            try
            {
                Dictionary<int, Dictionary<String, String>> ShowTimeDictionary = MBSCache.getShowTimeDictionary();
                Dictionary<String, String> showTimeSeatDictionary = ShowTimeDictionary[showTimeId];
                List<String> selectedSeatsList = seatList;
                using (TransactionScope tnx = new TransactionScope())
                {
                    foreach (String seat in selectedSeatsList)
                    {
                        showTimeSeatDictionary[seat] = "Blocked";

                    }
                //    MBSCache.setShowTimeDictionary(ShowTimeDictionary);
                    tnx.Complete();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool bookAvailableSeatsInCache(int showTimeId, List<String> seatList)
        {
            try
            {
                Dictionary<int, Dictionary<String, String>> ShowTimeDictionary = MBSCache.getShowTimeDictionary();
                Dictionary<String, String> showTimeSeatDictionary = ShowTimeDictionary[showTimeId];
                List<String> selectedSeatsList = seatList;
                using (TransactionScope tnx = new TransactionScope())
                {
                    foreach (String seat in selectedSeatsList)
                    {
                        showTimeSeatDictionary[seat] = "Booked";

                    }
                    //    MBSCache.setShowTimeDictionary(ShowTimeDictionary);
                    tnx.Complete();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            return true;
        }
        
        
        
    }
}
